﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Surface.Core;
using Microsoft.Surface.Core.Manipulations;
using Microsoft.Xna.Framework;

namespace Sublimation
{
    public class Tower_manipulator : GeneralManipulator
    { 
        public Tower_manipulator(Game game,Vector2 position, WorldPropertiesSurf worldProperties, GameObject owner): base(game,position.X,position.Y,1,0,owner,false,false,true,false)
        {
            this.worldProperties = worldProperties;
            addedContacts = new List<Contact>();
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            ReadOnlyContactCollection currentContacts = worldProperties.Contacts;
            foreach (Contact contact in currentContacts)
            {
                if (!((WorldPropertiesSurf)worldProperties).Owned.Contains(contact.Id) && contact.IsFingerRecognized && ((TowerSurf)owner).belongs(contact))
                {
                    add_contact(contact);
                    ((WorldPropertiesSurf)worldProperties).Owned.Add(contact.Id);
                    break;
                }
            }
            //check for changes in contacts since the last update
            bool changed = false;
            //a new contact is there
            if (addedContacts.Count > 0)
            {
                changed = true;
            }
            // Create a list for removed manipulators.
            List<Manipulator> removedManipulators = new List<Manipulator>();

            Contact temp_contact;
            if (previousContacts != null)
            {
                // Find all the contacts that were removed since the last check. 
                foreach (Contact contact in previousContacts)
                {

                    if (currentContacts.TryGetContactFromId(contact.Id, out temp_contact))
                    {
                        //one old contact has moved
                        if (contact.FrameTimestamp != temp_contact.FrameTimestamp)
                        {
                            changed = true;
                        }
                        add_contact(temp_contact);
                    }
                    else
                    {
                        changed = true;
                        removedManipulators.Add(new Manipulator(contact.Id, contact.CenterX, contact.CenterY));
                    }
                }

            }

            // Create a list for the current manipulators.
            List<Manipulator> currentManipulators = new List<Manipulator>();

            //virtual fixed center

            if (addedContacts.Count > 0)
            {
                float h = (float)Math.Sqrt((addedContacts.First<Contact>().CenterX - manipulationXPoint) * (addedContacts.First<Contact>().CenterX - manipulationXPoint) + (addedContacts.First<Contact>().CenterY - manipulationYPoint) * (addedContacts.First<Contact>().CenterY - manipulationYPoint));
                float a = (addedContacts.First<Contact>().CenterX - manipulationXPoint);
                float o = (addedContacts.First<Contact>().CenterY - manipulationYPoint);


                if (h != 0)
                {
                    float offsetX = -20 * a / h;
                    float offsetY = -20 * o / h;


                    currentManipulators.Add(new Manipulator(1, manipulationXPoint + offsetX, manipulationYPoint + offsetY));
                }
            }

            // Transform the current contacts to Manipulators.
            foreach (Contact contact in addedContacts)
            {
                currentManipulators.Add(new Manipulator(contact.Id, contact.CenterX, contact.CenterY));
            }

            // Save contacts for the next comparison.
            previousContacts = addedContacts;


            ((Tower)owner).Mouving = addedContacts.Count > 0;
           
            if (changed)
            {
                // Raise Manipulation events, if appropriate.
               ManipulationProcessor.ProcessManipulators(currentManipulators, removedManipulators);
               owner.Rotation = manipulationRotation;
            }
            addedContacts = new List<Contact>();
        }
    }
}
